﻿using Autodesk.AutoCAD.EditorInput;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace AutoCadConvert.Common
{
    class UserJig : DrawJig
    {
        private List<Entity> entities = new List<Entity>();
        private Point3d moveStartPnt;
        private Point3d moveEndPnt;
        private Double rotateAngle;
        private Double scaleFactor;

        public UserJig(Point3d basePnt)
        {
            moveStartPnt = basePnt;
            moveEndPnt = moveStartPnt;
            rotateAngle = 0;
            scaleFactor = 1;
        }
        public void AddEntity(Entity ent)
        {
            entities.Add(ent);
        }
        public Matrix3d Transformation
        {
            get
            {
                return Matrix3d.Scaling(scaleFactor, moveEndPnt).
                    PostMultiplyBy(Matrix3d.Rotation(rotateAngle, Vector3d.ZAxis, moveEndPnt)).
                    PostMultiplyBy(Matrix3d.Displacement(moveStartPnt.GetVectorTo(moveEndPnt)));
            }
        }
        public void TransformEntities()
        {
            Matrix3d mat = Transformation;
            foreach (Entity ent in entities)
            {
                ent.TransformBy(mat);
            }
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            //switch (step)
            //{
            //    case 1:
            //        JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nMove:");
            //        prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode
            //            | UserInputControls.GovernedByUCSDetect;
            //        PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
            //        if (prResult1.Status != PromptStatus.OK)
            //            return SamplerStatus.Cancel;

            //        if (prResult1.Value.Equals(moveEndPnt))
            //        {
            //            return SamplerStatus.NoChange;
            //        }
            //        else
            //        {
            //            moveEndPnt = prResult1.Value;
            //            return SamplerStatus.OK;
            //        }
            //case 2:
            //    JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nRotate:");
            //    prOptions2.UseBasePoint = true;
            //    prOptions2.BasePoint = moveEndPnt;
            //    prOptions2.UserInputControls = UserInputControls.GovernedByOrthoMode
            //        | UserInputControls.GovernedByUCSDetect;
            //    PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
            //    if (prResult2.Status != PromptStatus.OK)
            //        return SamplerStatus.Cancel;

            //    if (prResult2.Value.Equals(rotateAngle))
            //    {
            //        return SamplerStatus.NoChange;
            //    }
            //    else
            //    {
            //        rotateAngle = prResult2.Value;
            //        return SamplerStatus.OK;
            //    }
            //case 3:
            //    JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nScale:");
            //    prOptions3.UseBasePoint = true;
            //    prOptions3.BasePoint = moveEndPnt;
            //    prOptions3.UserInputControls = UserInputControls.GovernedByOrthoMode
            //        | UserInputControls.GovernedByUCSDetect;
            //    PromptDoubleResult prResult3 = prompts.AcquireDistance(prOptions3);
            //    if (prResult3.Status != PromptStatus.OK)
            //        return SamplerStatus.Cancel;

            //    if (prResult3.Value.Equals(scaleFactor))
            //    {
            //        return SamplerStatus.NoChange;
            //    }
            //    else
            //    {
            //        scaleFactor = prResult3.Value;
            //        return SamplerStatus.OK;
            //    }

            //    default:
            //        break;
            //}
            //return SamplerStatus.OK;
            JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nSpecify the position:");
            prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode
                | UserInputControls.GovernedByUCSDetect;
            PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
            if (prResult1.Status != PromptStatus.OK)
                return SamplerStatus.Cancel;

            if (prResult1.Value.Equals(moveEndPnt))
            {
                return SamplerStatus.NoChange;
            }
            else
            {
                moveEndPnt = prResult1.Value;
                return SamplerStatus.OK;
            }
        }

        protected override bool WorldDraw(WorldDraw draw)
        {
            Matrix3d mat = Transformation;
            WorldGeometry geo = draw.Geometry;
            if (geo != null)
            {
                geo.PushModelTransform(mat);
                foreach (Entity ent in entities)
                    geo.Draw(ent);
                geo.PopModelTransform();
            }
            return true;
        }
    }
}
